<script language="JavaScript1.2" src='<TMPL_VAR name=APP_PATH>/js/functions.js' /></script>
<link rel=stylesheet href=<TMPL_VAR name=APP_PATH>/css/style.css type=text/css>
<div id=editor_form>
<form method=post action=editor.cgi name=content_form>
<table width=100% border="0">
    <TR>
    <TD><h3>Content Editor</h3><div id=content_status></div></TD>
    <TD style="text-align:right;">
    <form action=editor.cgi>
        <select name=template onChange="get_template(this.value)"  style="width:200px;">
            <option>-- New Content from Template --</option>
            <TMPL_LOOP name=TEMPLATES>
                <option value=<TMPL_VAR name=template_id>><TMPL_VAR name=template_id>. <TMPL_VAR name=subject></option>
            </TMPL_LOOP>
        </select> | 
    </form>
    <div id=action_result style="text-align:right;float:left;padding-left:1em; border:0px solid grey;"></div>
        <input type=button id=btnNewWindow value='New Window' onClick="window.open('<TMPL_VAR name=APP_PATH_CGI>/admin/editor.cgi?content_id=<TMPL_VAR name=content_id>', 'Content Editor', 'width=800px,height=800px,resizable=1');"> |
        <input type=button id=btnPreview value='Preview' onClick="PreviewContent();"> | 
        <input type="button" value=Save onClick="save_content();">
        <input type=hidden id=action name=action value=''>
        <input type="hidden" name=content_id value=<TMPL_VAR name=content_id>><br>
        <a style="font-size:10px;" onClick="javascript:save_as_template();">Save this content as Template</a> | <a href=?action=New style="font-size:10px;">Create new content afresh</a>
    </TD>
    </TR>
</table>

<table width=100% cellpadding="0" cellspacing="3" border=0>
    <tr>
    <td class=smalllabel>Title</td>
    <TD class=smalllabel>Subject of Newsletter</TD>
    </tr>
    <TR>
    <TD width=50%>
            <input id=title name=title style="width:95%;border:1px lightgrey solid;" value='<TMPL_VAR name=title>'>
    </TD>
    <TD width=50% style="margin-left:1em;">
            <input id=subject name=subject style="width:100%;border:1px lightgrey solid;" value='<TMPL_VAR name=subject>'>
    </TD>
    </tr>
    <TR>
            <TD colspan="2">
                    <textarea id=content_editor name=content_editor style="width:100%;height:450;"><TMPL_VAR name=content></textarea>
            </TD>
    </TR>
</table>
</form>
</div>

<TMPL_INCLUDE name=../htmlheader.html>
<script>
var content_id	= '<TMPL_VAR name=content_id>';
if (content_id == '') 
{
        btnNewWindow.disabled = true;
        btnPreview.disabled 	= true;
}

// Abandoning this temporarily due to incompatibilities between TinyMCE and Prototype.js
function save_content(content_id)
{
    $('action').value = 'savecontent';
    document.forms['content_form'].submit();

//     var URL     = "editor.cgi";
//     var content = encodeURIComponent(document.getElementById('content_editor').value);
// 
//     PARAMS	= "action=savecontent" + 
//                 "&content_editor=" + content + 
//                 "&subject=" + escape($('subject').value) + 
//                 "&title=" + escape($('title').value) + 
//                 "&content_id=" + content_id;
//     // prompt ('', "c_id = " + PARAMS);
//     var myAjax = new Ajax.Request (
//                     URL, {
//                             method: 'POST', 
//                             parameters: PARAMS, 
//                             asynchronous: "true",
//                             onLoading: print_user_message('Saving Content..'),
//                             onSuccess: showResponse,
//                     });
}

function showResponse(origRequest) {
//         $(content_status).innerHTML = "orig text = " + origRequest.responseText;
    print_popup(origRequest.responseText);
}

function PreviewContent() {
    URL     = 'ContentViewer?content_id=<TMPL_VAR name=content_id>';
    window.open(URL, '', 'width=800,height=600,resizable=1,');
    return;
}

//	-- This is not currently working because 'newsletter_list' is an element in a different frame
function RefreshList () {
        URL	= "editor.cgi";
        PARAMS	= "refreshlist=true";
        new Ajax.Updater ('newsletter_list', URL, {method: 'get', parameters: PARAMS});
}

function PrintStatus(element, message) {
        // Thanks to ajaxload.info for the cool ajax loader gif.
        // See also http://www.sanbaldo.com/wordpress/1/ajax_gif/
        $(element).innerHTML = '';
        $(element).innerHTML = message;
        return;
}

function save_as_template() 
{
    $('action').value = 'saveastemplate';
    document.forms['content_form'].submit();
//     forward_to_content('<TMPL_VAR name=content_id>');
//     get_template();

//     URL     = "editor.cgi";
//     content = escape($('content_editor').value);

//     PARAMS  = "action=saveastemplate" + 
//               "&subject=" + escape($('subject').value) +
//               "&content=" + content;

//     var myAjax = new Ajax.Request (
//                     URL, {
//                             method: 'get', 
//                             parameters: PARAMS, 
//                             asynchronous: "true",
//                             onLoading: print_user_message('Saving as template...'),
//                             onSuccess: forward_to_content('<TMPL_VAR name=content_id>'),
//                     });

//     new Ajax.Updater (
//         'action_result',
//         URL, {
//             method: "get",
//             asynchronous: true,
//             parameters: PARAMS,
//             onLoading: print_user_message('Saving as template...'),
//             onSuccess: forward_to_content('<TMPL_VAR name=content_id>'),
//         }
//     );
}

function forward_to_content(content_id) {
    URL = "editor.cgi";
    if (content_id != '')
        window.location = URL + '?content_id=' + content_id;
}

function startExec() {
  new PeriodicalExecuter(function() {
  if (!confirm('Want me to annoy you again later?'))
    this.stop();
  }, 5);

}

function get_template(template_id) 
{
    window.location = "editor.cgi?action=New_from_template&t_id=" + template_id;
    return;
}
</script>


<script>
tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "table,advimage,advlink,emotions,iespell,insertdatetime,zoom,flash,searchreplace,print,contextmenu,fullscreen,fullpage",
        theme_advanced_buttons1_add_before : "save,separator",
        theme_advanced_buttons1_add : "fontselect,fontsizeselect",
        theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor",
        theme_advanced_buttons2_add_before: "cut,copy,paste,separator,search,replace,separator",
        theme_advanced_buttons3_add_before : "tablecontrols,separator",
        theme_advanced_buttons3_add : "emotions,iespell,flash,advhr,separator,print,fullscreen,fullpage",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advancecleanupd_path_location : "bottom",
        plugin_insertdate_dateFormat : "%Y-%m-%d",
        plugin_insertdate_timeFormat : "%H:%M:%S",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
        external_link_list_url : "example_data/example_link_list.js",
        external_image_list_url : "example_data/example_image_list.js",
        flash_external_list_url : "example_data/example_flash_list.js",
        verify_html:"false"
});
</script>